package com.zrwl.poscloud.commons.utils;

import org.junit.Test;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.Properties;

@Component
@Service
public class JavaMailWithFileUtils {

        // 初始化参数
        private static Properties prop;
        // 发件人
        private static InternetAddress sendMan = null;
        static {
            prop = new Properties();
            prop.put("mail.transport.protocol", "smtp"); // 指定协议
            prop.put("mail.smtp.host", "smtp.qq.com"); // 主机 stmp.qq.com
            // prop.put("mail.smtp.port", 25); // 端口
            prop.put("mail.smtp.auth", "true"); // 用户密码认证
            prop.put("mail.debug", "true"); // 调试模式
            try {
                sendMan = new InternetAddress("1273692478@qq.com");
            } catch (AddressException e) {
                throw new RuntimeException(e);
            }
        }


        @Test
        public void testSend() throws Exception {

            // 1. 创建邮件会话
            Session session = Session.getDefaultInstance(prop);
            // 2. 创建邮件对象
            MimeMessage message = new MimeMessage(session);
            // 3. 设置参数：标题、发件人、收件人、发送时间、内容
            message.setSubject("带图片邮件");
            message.setSender(sendMan);
            message.setRecipient(RecipientType.TO, new InternetAddress("2771768682@qq.com"));
            message.setSentDate(new Date());

            /*
             * 带附件(图片)邮件开发
             */
            // 构建一个总的邮件块
            MimeMultipart mixed = new MimeMultipart("mixed");
            // ---> 总邮件快，设置到邮件对象中
            message.setContent(mixed);
            // 左侧： （文本+图片资源）
            MimeBodyPart left = new MimeBodyPart();
            // 右侧： 附件
            MimeBodyPart right = new MimeBodyPart();
            // 设置到总邮件块
            mixed.addBodyPart(left);
            mixed.addBodyPart(right);

            /****** 附件 ********/
            String attr_path = "C:\\xiangmu\\11_message\\src\\main\\resources\\a.docx";
            DataSource attr_ds = new FileDataSource(new File(attr_path));
            DataHandler attr_handler = new DataHandler(attr_ds);
            right.setDataHandler(attr_handler);
            right.setFileName("a.docx");

            /*************** 设置邮件内容: 多功能用户邮件 (related) *******************/
            // 4.1 构建一个多功能邮件块
            MimeMultipart related = new MimeMultipart("related");
            // ----> 设置到总邮件快的左侧中
            left.setContent(related);

            // 4.2 构建多功能邮件块内容 = 左侧文本 + 右侧图片资源
            MimeBodyPart content = new MimeBodyPart();
            MimeBodyPart resource = new MimeBodyPart();

            // 设置具体内容: a.资源(图片)
            String filePath = "C:\\xiangmu\\11_message\\src\\main\\resources\\b.png";
            DataSource ds = new FileDataSource(new File(filePath));
            DataHandler handler = new DataHandler(ds);
            resource.setDataHandler(handler);
            resource.setContentID("b.png"); // 设置资源名称，给外键引用

            // 设置具体内容: b.文本
            content.setContent("测试程序带图片加附件<img src='cid:b.png'/>", "text/html;charset=UTF-8");

            related.addBodyPart(content);
            related.addBodyPart(resource);

            // 5. 发送
            Transport trans = session.getTransport();
            trans.connect("1273692478@qq.com", "krnnjyvpjweibabg");
            trans.sendMessage(message, message.getAllRecipients());
            trans.close();
        }
    }


